﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Configuration;

namespace Core.DMS
{
    public class Folder : FileSystemObject, IEnumerable
    {
        List<FileSystemObject> _children = null;
        ViewMode _view = ViewMode.List;

        public int Children
        {
            get
            {
                if (_children == null)
                    GetChildren();

                return _children.Count;
            }
        }

        public ViewMode DefaultViewMode { get { return _view; } set { _view = value;} }

        public static Folder Root { get { return new Folder(-1, "Root"); } }

        public Folder(int Parent, string Title) :base(Parent, Title)
        {

        }

        internal Folder(IDataReader data) : base(data)
        {
            
        }

        public static Folder Get(int FolderID)
        {
            if(FolderID != -1)
                return AppFunctions.Documents.GetFolder(FolderID);

            return Folder.Root;
        }

        public void Save()
        {
            _id = AppFunctions.Documents.SaveFolder(this);
        }

        public IEnumerator GetEnumerator()
        {
            if (_children == null)
                _children = AppFunctions.Documents.GetFolderContents(ID);

            return _children.GetEnumerator();
        }

        public FileSystemObject[] GetChildren()
        {
            if (_children == null)
                _children = AppFunctions.Documents.GetFolderContents(ID);

            return _children.ToArray();
        }
    }
}
